<template>
  <el-drawer
    :title="!dataForm.id ? '新增' : '修改'"
    :visible="skuVisible"
    size="90vw"
    @close="closeDialog"
    append-to-body
  >
    <div style="margin: 20px 40px">
      <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="100px">
        <el-collapse v-model="activeNames">
          <el-collapse-item name="1">
            <template slot="title">
              <div>
                <i class="header-icon el-icon-goods"></i>
                <span class="title-size">基本信息</span>
              </div>
            </template>
            <el-form-item label="商品图片" prop="brandName">
                <ImagesUpload></ImagesUpload>
            </el-form-item>
            <el-form-item label="商品名称" prop="goodsName">
              <el-input v-model="dataForm.goodsName" placeholder="商品名称" ></el-input>
            </el-form-item>
          </el-collapse-item>
          <el-collapse-item title="反馈 Feedback" name="2">
            <div>控制反馈：通过界面样式和交互动效让用户可以清晰的感知自己的操作；</div>
            <div>页面反馈：操作后，通过页面元素的变化清晰地展现当前状态。</div>
          </el-collapse-item>
          <el-collapse-item title="效率 Efficiency" name="3">
            <div>简化流程：设计简洁直观的操作流程；</div>
            <div>清晰明确：语言表达清晰且表意明确，让用户快速理解进而作出决策；</div>
            <div>帮助用户识别：界面简单直白，让用户快速识别而非回忆，减少用户记忆负担。</div>
          </el-collapse-item>
          <el-collapse-item title="可控 Controllability" name="4">
            <div>用户决策：根据场景可给予用户操作建议或安全提示，但不能代替用户进行决策；</div>
            <div>结果可控：用户可以自由的进行操作，包括撤销、回退和终止当前操作等。</div>
          </el-collapse-item>
        </el-collapse>
      </el-form>
    </div>
     <div class="dialog__footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary">确 定</el-button>
      </div>
  </el-drawer>
</template>

<script>
import { getObj, addObj, putObj } from "@/api/shop/goods/sku";
import ImagesUpload from "@/components/imagesUpload";

export default {
  props:{
    skuVisible: {
      type: Boolean,
      default: false
    },
  },
  data() {
    return {
      activeNames:['1', '2', '3', '4'],
      canSubmit: false,
      dataForm: {},
      dataRule: {
        brandName: [
          { required: true, message: "品牌名称不能为空", trigger: "blur" },
        ],
        goodsName: [
          { required: true, message: "商品名称不能为空", trigger: "blur" },
        ]
      },
    };
  },
  components: {
    ImagesUpload
  },
  methods: {
    init(id) {
      this.canSubmit = true;
      if(id){
        getObj(id).then((response) => {
          this.dataForm = response.data.data;
        });
      }
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          if (this.dataForm.id) {
            putObj(this.dataForm)
              .then((data) => {
                this.$notify.success("修改成功");
                this.$emit("refreshDataList");
                this.closeDialog()
              })
              .catch(() => {
                this.canSubmit = true;
              });
          } else {
            addObj(this.dataForm)
              .then((data) => {
                this.$notify.success("添加成功");
                this.$emit("refreshDataList");
                this.closeDialog()
              })
              .catch(() => {
                this.canSubmit = true;
              });
          }
        }
      });
    },
    //重置表单
    closeDialog() {
      this.$emit('update:skuVisible', false)
    },
  },
};
</script>

<style scoped>

.header-icon{
  font-size: 16px;
  margin-right: 10px;
}

.title-size{
  font-size: 16px;
}

.dialog__footer {
  display: block;
  padding: 10px 16px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  border-top: 1px solid #f0f0f0;
  width: 100%;
  position: absolute;
  left: 0;
  bottom: 0;
  background-color: #fff;
  text-align: right;
}
</style>
